#include "nexttodotable.h"



nextToDoTable::nextToDoTable(QTableWidget *table,abstractTable *parent): abstractTable(table, parent)
{
//    m_table = table;

    m_table->clear();
    m_table->setColumnCount(8);
    // 隐藏行号
    QHeaderView *header = m_table->verticalHeader();
    header->setHidden(true);
//    header->setSectionResizeMode(QHeaderView::Stretch);
    m_table->setColumnWidth(STATE, 35);
    m_table->setColumnWidth(ID, 25);
    m_table->setColumnHidden(ID, true);
    m_table->setColumnWidth(DELETE, 35);
    m_table->setColumnWidth(NAME, 160);


    // 设置单元格无法选中
//    _table->setSelectionMode(QAbstractItemView::NoSelection);
    // 以行为单位选中
    m_table->setSelectionBehavior ( QAbstractItemView::SelectRows);
    m_table->setSelectionMode ( QAbstractItemView::SingleSelection);
//    _table->setSelectionMode ( QAbstractItemView::NoSelection);
//    qDebug() << "table "<<sizeof(*table);
    m_table->setFont(QFont("宋体"));
    //设置表格中每一行的表头
    m_table->setHorizontalHeaderLabels(QStringList() << "状态" <<"ID"<< "任务名" << "分类" <<"每日番茄数"<< "今日已完成" << "剩余天数" << "操作");
    //设置表格数据区内的所有单元格都不允许编辑
    m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);

    m_table->setShowGrid(false);
    m_table->setWindowFlags(Qt::FramelessWindowHint);


    QSize size(15,15);
    m_pix_trash = QPixmap(":/pics/delete.png").scaled(size);
    m_pix_ready = QPixmap(":/pics/ready.png").scaled(size);
    m_pix_not_ready = QPixmap(":/pics/not_ready.png").scaled(size);
    m_pix_achieve = QPixmap(":/pics/achieve.png").scaled(size);





}


nextToDoTable::nextToDoTable(abstractTable *parent):abstractTable(parent)
{
    m_table = nullptr;
    QSize size(15,15);
    m_pix_trash = QPixmap(":/pics/delete.png").scaled(size);
    m_pix_ready = QPixmap(":/pics/ready.png").scaled(size);
    m_pix_not_ready = QPixmap(":/pics/not_ready.png").scaled(size);
}

void nextToDoTable::addTask(taskItem &task)
{
    // 设定日期过期时不显示

    if (task.m_days != -1 && task.remainDay() < 0) {
        return ;
    }


//    int newRow = _table->rowCount();
    int newRow = getRowWithSort(task);
    m_table->insertRow(newRow);

    QTableWidgetItem * widget_item;
    qDebug() << task.m_taskName << task.m_eachDayTomatoes << task.m_todayAchieveTomato << endl;

    // state
    QLabel *state = new QLabel();
    // 设置鼠标在label上的形状
    state->setCursor(Qt::PointingHandCursor);
    if (m_table->rowCount() == 1){
        if (task.m_todayAchieveTomato >= task.m_eachDayTomatoes)
            state->setPixmap(m_pix_achieve);
        else
            state->setPixmap(m_pix_ready);
        state->setToolTip("selected");
        emit selectCurrentTask(task);
    } else{
        state->setPixmap(m_pix_not_ready);
        state->setToolTip("unselected");
    }


    state->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setCellWidget(newRow, STATE, state);

    // id
    widget_item = new QTableWidgetItem(QString("%1").arg(task.m_id));
    m_table->setItem(newRow,ID, widget_item);
    // taskName
    widget_item = new QTableWidgetItem(QString(task.m_taskName));
    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setItem(newRow,NAME, widget_item);
    // sort
    widget_item = new QTableWidgetItem(taskItem::sortArray[task.m_sort]);
    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setItem(newRow,SORT, widget_item);

    // everyday tomatoes

    widget_item = new QTableWidgetItem(QIcon(":/pics/tomato.png"),QString("%1")
                                   .arg(task.m_eachDayTomatoes));
    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setItem(newRow,TOMATO, widget_item);

    // today achieve tomatoes

    widget_item = new QTableWidgetItem(QIcon(":/pics/tomato.png"),QString("%1")
                                       .arg(task.todayAchieveTomatoes()));
    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    m_table->setItem(newRow,TODAY, widget_item);
    // remain days

    widget_item = new QTableWidgetItem(task.remainDay_str());
    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setItem(newRow,REMAINDAY, widget_item);

    // 删除按钮

    QLabel *trash = new QLabel();
    // 设置鼠标在label上的形状
    trash->setCursor(Qt::PointingHandCursor);
    trash->setPixmap(m_pix_trash);
    trash->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setCellWidget(newRow, DELETE, trash);



}

void nextToDoTable::removeTask(int row)
{
    m_table->removeRow(row);
}

int nextToDoTable::getNextTaskRow()
{
    // 获取当前选择行
    int row=0;
    for (int i=0; i<m_table->rowCount(); ++i){
        QLabel *label_item = (QLabel *)m_table->cellWidget(i, nextToDoTable::STATE);
        if (label_item->toolTip() == "selected"){
            row = i;
            break;
        }
    }
    if (row == m_table->rowCount()-1) row = -1;
    else row += 1;

    return row;

}

void nextToDoTable::updateTask(taskItem &task)
{
    int id;
    QLabel *state_label;
    for (int r=0; r<m_table->rowCount(); ++r)
    {
        id = m_table->item(r, ID)->text().toInt();
        if (id == task.m_id)
        {
            //NAME,SORT,TOMATO,TODAY,REMAINDAY
            m_table->item(r, NAME)->setText(task.m_taskName);
            m_table->item(r, SORT)->setText(taskItem::sortArray[task.m_sort]);
            m_table->item(r, TOMATO)->setText(QString("%1").arg(task.m_eachDayTomatoes));
            m_table->item(r, TODAY)->setText(QString("%1").arg(task.m_todayAchieveTomato));
            m_table->item(r, REMAINDAY)->setText(task.remainDay_str());

            if (task.m_todayAchieveTomato >= task.m_eachDayTomatoes)
            {
                state_label = (QLabel *)m_table->cellWidget(r, STATE);
                state_label->setPixmap(m_pix_achieve);

            }
            break;
        }
    }
    qDebug() << "to do list update task";
}

int nextToDoTable::getRowWithSort(const taskItem &task)
{
    int sort = task.m_sort;
    QString sort_text;
    for (int row=0; row < m_table->rowCount(); ++row)
    {
        sort_text = m_table->item(row, SORT)->text();
        qDebug() << "sort_text " << sort_text;
        if (sort < taskItem::alterSort2Int(sort_text))
        {
            return row;
        }

    }
    return m_table->rowCount();

}

int nextToDoTable::getId(int row)
{
    if (row < 0 || row >= m_table->rowCount()) return -1;
    return m_table->item(row, ID)->text().toInt();
}

QString nextToDoTable::getName(int row)
{
    if (row < 0 || row >= m_table->rowCount()) return "";
    return m_table->item(row, NAME)->text();
}
